package com.gking.centerSystem.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.centerSystem.entity.Routes;
import com.gking.centerSystem.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 分页条件查询用户
     *
     * @param page        分页信息
     * @param roleName    角色名称
     * @param account     账号
     * @param nickName    昵称
     * @param phoneNumber 手机号
     * @param email       邮箱
     * @param enable      是否启用
     * @return s
     */
    @Select("<script>" +
            "SELECT DISTINCT  u.id, u.account, u.nickname, u.avatar, u.phoneNumber, u.email, u.enable ,u.createTime " +
            "FROM user u " +
            "Left JOIN sy_user_roles userRole ON u.id = userRole.userId " +
            "Left JOIN sy_roles r ON userRole.roleId = r.id " +
            "WHERE " +
            "u.isDelete = 0 " +
            "<if test='roleName != null and roleName.trim() neq \"\"'> AND (r.name LIKE CONCAT('%', #{roleName}, '%') OR r.realName LIKE CONCAT('%', #{roleName}, '%')) </if>" +
            "<if test='account != null and account.trim() neq \"\"'> AND u.account LIKE CONCAT('%', #{account}, '%') </if>" +
            "<if test='nickName != null and nickName.trim() neq \"\"'> AND u.nickName LIKE CONCAT('%', #{nickName}, '%') </if>" +
            "<if test='phoneNumber != null and phoneNumber.size() > 0'> " +
            "<foreach collection='phoneNumber' item='phone' open=' AND (' separator=' OR ' close=') '>" +
            "u.phoneFuzzyQuery LIKE CONCAT('%', #{phone}, '%')" +
            "</foreach>" +
            "</if>" +
            "<if test='email != null and email.size() > 0'> " +
            "<foreach collection='email' item='emailItem' open=' AND (' separator=' OR ' close=') '>" +
            "u.emailFuzzyQuery LIKE CONCAT('%', #{emailItem}, '%')" +
            "</foreach>" +
            "</if>" +
            "<if test='enable != null'> AND u.enable = #{enable}</if>" +
            "ORDER BY u.createTime DESC" +
            "</script>")
    @InterceptorIgnore(tenantLine = "true")
    IPage<User> findUsersByCriteria(Page<User> page,
                                    @Param("roleName") String roleName,
                                    @Param("account") String account,
                                    @Param("nickName") String nickName,
                                    @Param("phoneNumber") List<String> phoneNumber,
                                    @Param("email") List<String> email,
                                    @Param("enable") Integer enable);


    /**
     * 分页简单条件查询用户
     *
     * @param page     分页信息
     * @param account  账号
     * @param nickName 昵称
     * @return s
     */
    @Select("<script>" +
            "SELECT DISTINCT u.id, u.account, u.nickname, u.avatar, u.phoneNumber, u.email, u.enable, u.createTime " +
            "FROM user u " +
            "WHERE u.isDelete = 0 " +
            "<if test='(account != null and account.trim() != \"\") or (nickName != null and nickName.trim() != \"\")'>" +
            " AND ( " +
            "   <if test='account != null and account.trim() != \"\"'>" +
            "     u.account LIKE CONCAT('%', #{account}, '%') " +
            "   </if>" +
            "   <if test='account != null and account.trim() != \"\" and nickName != null and nickName.trim() != \"\"'>" +
            "     OR " +
            "   </if>" +
            "   <if test='nickName != null and nickName.trim() != \"\"'>" +
            "     u.nickName LIKE CONCAT('%', #{nickName}, '%') " +
            "   </if>" +
            " ) " +
            "</if>" +
            " ORDER BY u.createTime DESC" +
            "</script>")
    @InterceptorIgnore(tenantLine = "true")
    IPage<User> findUsersByEasy(Page<User> page,
                                @Param("account") String account,
                                @Param("nickName") String nickName);


    /**
     * 获取用户系统访问路由
     *
     * @param userId       用户id
     * @param roleSortList 访问的系统角色分类
     * @return l
     */
    @Select("<script>" +
            "SELECT DISTINCT rt.id, rt.path, rt.name, rt.component, rt.componentPath, rt.level, " +
            "rt.parentId, rt.sortId, rt.enable, rt.description, rt.isRedirect, rt.isNavigation, rt.systemCode " +
            "FROM sy_routes rt " +
            "JOIN sy_role_routes rr ON rr.routeId = rt.id " +
            "JOIN sy_roles r ON r.id = rr.roleId " +
            "LEFT JOIN sy_user_roles ur ON ur.roleId = r.id " +
            "LEFT JOIN user u ON u.id = ur.userId " +
            "JOIN sy_sort s ON s.id = r.sortId " +
            "WHERE " +
            "rt.isDelete = 0 AND rt.enable = 0 " +
            "AND u.id = #{userId} " +
            "<foreach collection='roleSortList' item='sortItem' open=' AND (' separator=' OR ' close=') '>" +
            "r.sortId = #{sortItem} " +
            "</foreach>" +
            "OR r.isDefault = 0 AND r.enable = 0 " +
            "AND r.sortId IN " +
            "<foreach collection='roleSortList' item='sortItem' open='(' separator=',' close=')'>" +
            "#{sortItem}" +
            "</foreach>" +
            "</script>")
    @InterceptorIgnore(tenantLine = "true")
    List<Routes> getUserAccessRoutes(@Param("userId") String userId,
                                     @Param("roleSortList") List<String> roleSortList);
}
